package com.mc.developmentkit.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.qamaster.android.R;
import com.mc.developmentkit.bean.AboutUsInfo;
import com.mc.developmentkit.bean.UserInfo;
import com.mc.developmentkit.utils.Primary;
import com.mc.developmentkit.config.MCConstant;
import com.mc.developmentkit.config.MCThird;
import com.mc.developmentkit.https.HttpUtils;
import com.mc.developmentkit.json.Json;
import com.mc.developmentkit.login.MCThirdPartyLogin;
import com.mc.developmentkit.utils.DbUtils;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.TitleManger;
import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * Created by LeBron on 2016/12/28.
 */

public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText et_Account, et_Password;
    private Button btn_Login;
    private String QQKey;
    private TextView toRegister, toFindPassword, qqNum, appText, address;
    private ImageView login_WeChat, login_QQ, join_QQqun;
    private RelativeLayout re_GameCenter, re_FuLiCenter;

    private AboutUsInfo aboutUs;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        TitleManger insetance = TitleManger.getInsetance();
        insetance.setContext(this);
        insetance.setName("登录");
        initView();
        initData();
    }

    @Override
    public void onClick(View v) {
        String account = et_Account.getText().toString();
        String password = et_Password.getText().toString();
        int i = v.getId();
        if (i == R.id.user_login) {
            if (account != null && password != null && !account.equals("") && !password.equals("")) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("account", account);
                    jsonObject.put("password", password);
                    HttpUtils.POST(loginHandler, MCConstant.getUserLoginUrl(), jsonObject.toString(), true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                ToastUtil.showToast("账号或密码不能为空");
            }
        } else if (i == R.id.to_Register) {
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        } else if (i == R.id.to_FindPassword) {
            startActivity(new Intent(LoginActivity.this, ForgetPasswordActivity.class));
        } else if (i == R.id.Login_WeChat) {
            MCThirdPartyLogin.getInstance().toLogin(LoginActivity.this, MCThird.Wechat, new Primary(LoginActivity.this, "wx"));
        } else if (i == R.id.Login_QQ) {
            MCThirdPartyLogin.getInstance().toLogin(LoginActivity.this, MCThird.QQ, new Primary(LoginActivity.this, "qq"));
        } else if (i == R.id.img_JohnQun) {
            MCUtils.joinQQGroup(LoginActivity.this,QQKey);
        } else if (i == R.id.re_GameCenter) {
            //MainActivity.radio_YouXi.setChecked(true);
        } else if (i == R.id.re_FuliCenter) {
            //MainActivity.redio_FuLi.setChecked(true);
        } else if (i == R.id.wangzhi) {
            if (!TextUtils.isEmpty(aboutUs.gNet)) {
                Intent intent = new Intent();
                intent.putExtra("url", aboutUs.gNet);
                intent.putExtra("title", "官网");
                intent.setClass(LoginActivity.this, AgreementActivity.class);
                startActivity(intent);
                finish();
            } else {
                ToastUtil.showToast("协议链接为空");
            }

        }
    }

    private void initData() {
        HttpUtils.GET(mHandler, MCConstant.getAboutUsUrl());
    }

    private void initView() {
        et_Account = (EditText) findViewById(R.id.et_account);
        et_Password = (EditText) findViewById(R.id.et_password);
        btn_Login = (Button) findViewById(R.id.user_login);
        toRegister = (TextView) findViewById(R.id.to_Register);
        toFindPassword = (TextView) findViewById(R.id.to_FindPassword);
        qqNum = (TextView) findViewById(R.id.qunhao);
        appText = (TextView) findViewById(R.id.AppText);
        address = (TextView) findViewById(R.id.wangzhi);
        login_WeChat = (ImageView) findViewById(R.id.Login_WeChat);
        login_QQ = (ImageView) findViewById(R.id.Login_QQ);
        join_QQqun = (ImageView) findViewById(R.id.img_JohnQun);
        re_GameCenter = (RelativeLayout) findViewById(R.id.re_GameCenter);
        re_FuLiCenter = (RelativeLayout) findViewById(R.id.re_FuliCenter);

        btn_Login.setOnClickListener(this);
        toRegister.setOnClickListener(this);
        toFindPassword.setOnClickListener(this);
        login_WeChat.setOnClickListener(this);
        login_QQ.setOnClickListener(this);
        join_QQqun.setOnClickListener(this);
        address.setOnClickListener(this);
        re_GameCenter.setOnClickListener(this);
        re_FuLiCenter.setOnClickListener(this);

    }

    /**
     * 登录界面信息    QQ群Key QQ群号  APP简介  官网地址
     */
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    aboutUs = Json.aboutUs(msg.obj.toString());
                    if (aboutUs != null) {
                        qqNum.setText(aboutUs.qunNum);
                        address.setText(aboutUs.gNet);
                        appText.setText(aboutUs.jianJie);
                        QQKey = aboutUs.qunKey;
                    } else {
                        ToastUtil.showToast("关于我们为空");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

    android.os.Handler loginHandler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    UserInfo userInfo = Json.DNSLogin("用户登录", msg.obj.toString());
                    if (userInfo != null) {
                        DbManager db = DbUtils.getDB();
                        try {
                            db.saveOrUpdate(userInfo);
                            startActivity(new Intent(LoginActivity.this, UserCenterActivity.class));
                            finish();
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    } else {
                        ToastUtil.showToast("用户信息为空");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

}
